<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>教师列表</title>
	<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
	<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
	<link rel="stylesheet" type="text/css" th:href="@{/easyui/css/demo.css}">
	<script type="text/javascript" th:src="@{/easyui/jquery.min.js}"></script>
	<script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
	<script type="text/javascript" th:src="@{/easyui/js/validateExtends.js}"></script>
	<script type="text/javascript" th:inline="javascript">
		$(function() {
			let table;
			$('#dataList').datagrid({
				title:'教师列表',
				iconCls:'icon-more',
				border: true,
				collapsible:false,
				fit: true,
				method: "post",
				url: "/teacher/list",
				idField:'id',
				singleSelect:false,//是否单选
				pagination:true,//分页控件
				rownumbers:true,//行号
				sortName:'id',
				sortOrder:'DESC',
				remoteSort: false,
				columns: [
						[
					{field:'chk',checkbox: true,width:50},
					{field:'id',title:'ID',width:250, sortable: true},
					{field:'nickName',title:'姓名',width:150},
					{field:'sex',title:'性别',width:100},
					{field:'mobile',title:'联系方式',width:150},
					{field:'classId',title:'专业',width:150,
						formatter: function(value,row,index){
							if (row.classId){
								let clazzList = $("#clazzList").combobox("getData");
								for(let i=0;i<clazzList.length;i++ ){
									if(row.classId === clazzList[i].id){
										return clazzList[i].name;
									}
								}
								return row.classId;
							} else {
								return 'not found';
							}
						}
					}
					]
				],
				toolbar: "#toolbar",
				onBeforeLoad : function(){
					try{
						$("#clazzList").combobox("getData")
					}catch(err){
						preLoadClazz();
					}
				}
			});

			//设置分页控件
			let p = $('#dataList').datagrid('getPager');
			$(p).pagination({
				pageSize: 10,
				pageList: [10,20,30,50,100],
				beforePageText: '第',
				afterPageText: '页    共 {pages} 页',
				displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			});

			//设置工具类按钮
			$("#add").click(function(){
				table = $("#addTable");
				$("#addDialog").dialog("open");
			});

			// 修改
			$("#edit").click(function(){
				table = $("#editTable");
				// 勾选的行号,并非数据库的教师id
				let selectRows = $("#dataList").datagrid("getSelections");
				if(selectRows.length !== 1){
					$.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
				} else{
					$("#editDialog").dialog("open");
				}
			});

			//删除
			$("#delete").click(function(){
				let selectRows = $("#dataList").datagrid("getSelections");
				let selectLength = selectRows.length;
				if(selectLength === 0){
					$.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
				} else{
					let jsonObj = {};
					$.each(selectRows,function(i,n){
						jsonObj["ids["+i+"]"] = n.id;
					});
					$.messager.confirm("消息提醒", "将删除与教师相关的所有数据，确认继续？", function(r){
						if(r){
							$.ajax({
								type: "post",
								url: "/user/delete",
								data: jsonObj,
								success: function(data){
									if(data.success){
										$.messager.alert("消息提醒",data.message,"info");
										//刷新表格
										$("#dataList").datagrid("reload");
										$("#dataList").datagrid("uncheckAll");
									} else{
										$.messager.alert("消息提醒",data.message,"warning");
									}
								}
							});
						}
					});
				}
			});

			function preLoadClazz(){
				$("#clazzList").combobox({
					width: "200",
					height: "30",
					valueField: "id",
					textField: "name",
					multiple: false, //可多选
					editable: false, //不可编辑
					method: "post",
					url: "/clazz/getClazzList?from=combox",
					onChange: function(newValue, oldValue){
					}
				});
			}

			//设置添加窗口
			$("#addDialog").dialog({
				title: "添加教师",
				width: 850,
				height: 550,
				iconCls: "icon-add",
				modal: true,
				collapsible: false,
				minimizable: false,
				maximizable: false,
				draggable: true,
				closed: true,
				buttons: [
					{
						text:'添加',
						plain: true,
						iconCls:'icon-user_add',
						handler:function(){
							let validate = $("#addForm").form("validate");
							if(!validate){
								$.messager.alert("消息提醒","错误信息或重复信息不可添加，请检查你输入的数据!","warning");
							} else{
								let formData = new FormData($( "#addForm" )[0]);

								$.ajax({
									type: "post",
									url: "/user/add",
									data: formData,
									async: false,
									cache: false,
									contentType: false,
									processData: false,
									success: function(data){
										if(data.success){
											$.messager.alert("消息提醒",data.message,"info");
											//关闭窗口
											$("#addDialog").dialog("close");
											//清空原表格数据
											$("#add_number").textbox('setValue', "");
											$("#add_name").textbox('setValue', "");
											$("#add_nickName").textbox('setValue', "");
											$("#add_sex").textbox('setValue', "男");
											$("#add_phone").textbox('setValue', "");
											$(table).find(".chooseTr").remove();

											//重新刷新页面数据
											$('#dataList').datagrid("reload");

										} else{
											$.messager.alert("消息提醒",data.message,"warning");
										}
									}
								});
							}
						}
					},
					{
						text:'重置',
						plain: true,
						iconCls:'icon-reload',
						handler:function(){
							$("#add_number").textbox('setValue', "");
							$("#add_name").textbox('setValue', "");
							$("#add_phone").textbox('setValue', "");

							$(table).find(".chooseTr").remove();

						}
					},
				],
				onClose: function(){
					$("#add_number").textbox('setValue', "");
					$("#add_name").textbox('setValue', "");
					$("#add_phone").textbox('setValue', "");

					$(table).find(".chooseTr").remove();
				}
			});



			//下拉框通用属性
			$("#edit_clazzList, #add_clazzList").combobox({
				width: "200",
				height: "30",
				valueField: "id",
				textField: "name",
				multiple: false, //不可多选
				editable: false, //不可编辑
				method: "post",
			});



			$("#add_clazzList").combobox({
				url: "/clazz/getClazzList?from=combox",
				onLoadSuccess: function(){
					//默认选择第一条数据
					let data = $(this).combobox("getData");
					$(this).combobox("setValue", data[0].id);
				}
			});

			// 编辑教师专业下拉框
			$("#edit_clazzList").combobox({
				url: "/clazz/getClazzList?from=combox",
				onLoadSuccess: function(){
					//默认选择第一条数据
					let data = $(this).combobox("getData");
					$(this).combobox("setValue", data[0].id);
				}
			});


			// 修改教师信息
			$("#editDialog").dialog({
				title: "修改教师信息",
				width: 850,
				height: 550,
				iconCls: "icon-edit",
				modal: true,
				collapsible: false,
				minimizable: false,
				maximizable: false,
				draggable: true,
				closed: true,
				buttons: [
					{
						text:'提交',
						plain: true,
						iconCls:'icon-user_add',
						handler:function(){
							var validate = $("#editForm").form("validate");
							if(!validate){
								$.messager.alert("消息提醒","请检查你输入的数据!","warning");
							} else{
								let object = $("#dataList").datagrid("getSelected");
								let formData = new FormData($( "#editForm" )[0]);
								formData.append("id", object.id);
								$.ajax({
									type: "post",
									url: "/user/update",
									data: formData,
									async: false,
									cache: false,
									contentType: false,
									processData: false,
									success: function(data){
										if(data.success){
											$.messager.alert("消息提醒",data.message,"info");
											//关闭窗口
											$("#editDialog").dialog("close");
											//清空原表格数据
											$("#edit_name").textbox('setValue', "");
											$("#edit_sex").textbox('setValue', "男");
											$("#edit_phone").textbox('setValue', "");

											//重新刷新页面数据
											$('#dataList').datagrid("reload");
											$('#dataList').datagrid("uncheckAll");
										} else {
											$.messager.alert("消息提醒",data.message,"warning");
										}
									}
								});
							}
						}
					},
					{
						text:'重置',
						plain: true,
						iconCls:'icon-reload',
						handler:function(){
							$("#edit_name").textbox('setValue', "");
							$("#edit_phone").textbox('setValue', "");

							$(table).find(".chooseTr").remove();

						}
					},
				],
				onBeforeOpen: function(){
					let selectRow = $("#dataList").datagrid("getSelected");

					//设置值
					$("#edit_name").textbox('setValue', selectRow.username);
					$("#edit_sex").textbox('setValue', selectRow.sex);
					$("#edit_phone").textbox('setValue', selectRow.mobile);

					$("#edit-id").val(selectRow.id);
					let classId = selectRow.classId;
					setTimeout(function(){
						$("#edit_clazzList").combobox('setValue', classId);
					}, 100);
				},
				onClose: function(){
					$("#edit_name").textbox('setValue', "");
					$("#edit_phone").textbox('setValue', "");
				}
			});

			//搜索按钮监听事件
			$("#search-btn").click(function(){
				$('#dataList').datagrid('load',{
					teacherName: $('#search_student_name').val(),
					classId: $("#clazzList").combobox('getValue') === '' ? 0 : $("#clazzList").combobox('getValue')
				});
			});

			$("#clear-btn").click(function () {
				$("#search_student_name").val("");
				$("#clazzList").combobox('setValue',"");
			})
		});
	</script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>
<!-- 工具栏 -->
<div id="toolbar">
	<div th:if="${session.loginUser.userType == '1'}" style="float: left;">
		<a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a>
	</div>
	<div th:if="${session.loginUser.userType == '1'}" style="float: left;" class="datagrid-btn-separator"></div>
	<div style="float: left;">
		<a id="edit" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
	</div>
	<div style="float: left;" class="datagrid-btn-separator"></div>
	<div th:if="${session.loginUser.userType == '1'}" style="float: left;">
		<a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a>
	</div>
	<div style="float: left;margin-top:4px;" class="datagrid-btn-separator" >
		&nbsp;&nbsp;姓名：<input id="search_student_name" class="easyui-textbox" name="search_student_name" style="width: 200px; height: 30px;"/>
	</div>
	<div style="margin-left: 10px;margin-top:4px;" >
		专业：<input id="clazzList" class="easyui-textbox" name="class" />
		<a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
		<a id="clear-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">清空搜索</a>
	</div>
</div>

<!-- 添加窗口 -->
<div id="addDialog" style="padding: 10px;">
	<form id="addForm" method="post" enctype="multipart/form-data">
		<input id="add_userType" name="userType" value="3" type="hidden" />
		<table id="addTable" border=0 style="width:800px; table-layout:fixed;" cellpadding="6" align="center" >
			<tr>
				<td style="width:80px">专业:</td>
				<td colspan="3">
					<input id="add_clazzList" style="width: 200px; height: 30px;" class="easyui-textbox" name="classId" />
				</td>
				<td style="width:80px"></td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td colspan="4"><input id="add_name" style="width: 200px; height: 30px;border-radius: 5px;" class="easyui-textbox" type="text" name="username" data-options="required:true, missingMessage:'请填写用户名'" /></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td colspan="4"><input id="add_password" style="width: 200px; height: 30px;border-radius: 5px;" class="easyui-textbox" type="password" name="password" data-options="required:true, missingMessage:'请填写密码'" /></td>
			</tr>
			<tr>
				<td>姓名:</td>
				<td colspan="4"><input id="add_nickName" style="width: 200px; height: 30px;border-radius: 5px;" class="easyui-textbox" type="text" name="nickName" data-options="required:true, missingMessage:'请填写姓名'" /></td>
			</tr>
			<tr>
				<td>性别:</td>
				<td colspan="4"><select id="add_sex" class="easyui-combobox" data-options="editable: false, panelHeight: 50, width: 60, height: 30" name="sex"><option value="男">男</option><option value="女">女</option></select></td>
			</tr>
			<tr>
				<td>联系方式:</td>
				<td colspan="4"><input id="add_phone" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="mobile" validType="mobile" /></td>
			</tr>
		</table>
	</form>
</div>

<!-- 修改窗口 -->
<div id="editDialog" style="padding: 10px">
	<form id="editForm" method="post" enctype="multipart/form-data">
		<table id="editTable" border=0 style="width:800px; table-layout:fixed;" cellpadding="6" >
			<tr>
				<td style="width:40px">专业:</td>
				<td colspan="3">
					<input id="edit_clazzList" style="width: 200px; height: 30px;" class="easyui-textbox" name="classId" />
				</td>
				<td style="width:80px"></td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td><input id="edit_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="username" data-options="required:true, missingMessage:'请填写用户名'" /></td>
			</tr>
			<tr>
				<td>性别:</td>
				<td><select id="edit_sex" class="easyui-combobox" data-options="editable: false, panelHeight: 50, width: 60, height: 30" name="sex"><option value="男">男</option><option value="女">女</option></select></td>
			</tr>
			<tr>
				<td>联系方式:</td>
				<td><input id="edit_phone" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="mobile" validType="mobile" /></td>
			</tr>
		</table>
	</form>
</div>
</body>
</html>